package com.hy.study.demo.complexquerydemo.service.impl;

import com.hy.study.demo.complexquerydemo.bean.business.OrganizationDTO;
import com.hy.study.demo.complexquerydemo.bean.dao.DaqsOrganizationDTO;
import com.hy.study.demo.complexquerydemo.bean.view.OrganizationQueryDTO;
import com.hy.study.demo.complexquerydemo.bean.view.QueryCombinationCondition;
import com.hy.study.demo.complexquerydemo.builder.QueryBuilder;
import com.hy.study.demo.complexquerydemo.dao.OrganizationDao;
import com.hy.study.demo.complexquerydemo.service.QueryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class QueryServiceImpl extends QueryBuilder implements QueryService {
    @Autowired
    private OrganizationDao organizationDao;

    @Override
    public List<OrganizationDTO> queryOrganization(OrganizationQueryDTO queryDTO) {
        Set<QueryCombinationCondition> conditions = queryDTO.getQueryCombinationConditions();
        DaqsOrganizationDTO searchDto = new DaqsOrganizationDTO();
        BeanUtils.copyProperties(queryDTO, searchDto);
        List<DaqsOrganizationDTO> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(conditions)) {
            //全部使用  AND  进行查询
            result.addAll(organizationDao.searchDaqsOrganization(searchDto));
        } else {
            String condition = super.condition(conditions,DaqsOrganizationDTO.class);
            result.addAll(organizationDao.searchOrganization(condition, searchDto));
        }
        return this.getOrganization(result);
    }

    private List<OrganizationDTO> getOrganization(List<DaqsOrganizationDTO> result) {
        List<OrganizationDTO> organizationDTOList = new ArrayList<OrganizationDTO>();
        if (!CollectionUtils.isEmpty(result)) {
            for (DaqsOrganizationDTO dto : result) {
                OrganizationDTO dt=new OrganizationDTO();
                BeanUtils.copyProperties(dto, dt);
               // dt.setBusinessRegistrationInfo();
                // dt.setCommunicationInfo();
                // ...
                //查询补齐
                organizationDTOList.add(dt);
            }
        }
        return organizationDTOList;
    }
}
